package com.cwt_taxi_xa;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cwt_taxi_xa.constants.Constants;
import com.cwt_taxi_xa.db.bll.LocalInfoBiz;
import com.cwt_taxi_xa.record.RecorderBiz;
import com.cwt_taxi_xa.util.FileOper;

import java.io.File;

public class DownloadData extends CWTBaseActivity implements OnClickListener{

	private TextView business_data_size,checkin_data_size,checkout_data_size,
			gps_data_size,recorder_data_size,driver_data_size,photo_data_size;
	private int businessCount,checkinCount,checkoutCount,gpsCount,recorderCount;
	private int businessCacheCount,checkinCacheCount,checkoutCacheCount,gpsCacheCount;
	public static Handler handler;
	public static boolean isExit = false;
	private LocalInfoBiz infoBiz;
	private RecorderBiz recorderBiz;
	private TextView business_cache_data_size,checkin_cache_data_size,
			checkout_cache_data_size,gps_cache_data_size,tv_record_size,tv_photo_size;
	private LinearLayout ll_business,ll_checkin,ll_checkout,ll_gps,ll_driver,
			ll_recorder,ll_photo,ll_params;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.download_data);

		infoBiz = LocalInfoBiz.getInstance();
		recorderBiz = new RecorderBiz(this);
		isExit = true;
		setupViews();
		addListener();
		initData();
	}

	private void initData() {
		// TODO Auto-generated method stub
		businessCount = infoBiz.getCount(0);
		checkinCount = infoBiz.getCount(1);
		checkoutCount = infoBiz.getCount(2);
		gpsCount = infoBiz.getCount(3);
		recorderCount = recorderBiz.getCount();

		business_data_size.setText(String.valueOf(businessCount)+" 条");
		checkin_data_size.setText(String.valueOf(checkinCount)+" 条");
		checkout_data_size.setText(String.valueOf(checkoutCount)+" 条");
		gps_data_size.setText(String.valueOf(gpsCount)+" 条");
		recorder_data_size.setText(String.valueOf(recorderCount)+" 条");
		driver_data_size.setText("0 条");
		photo_data_size.setText("0 条");

		businessCacheCount = infoBiz.getNoPostCount(0);
		checkinCacheCount = infoBiz.getNoPostCount(1);
		checkoutCacheCount = infoBiz.getNoPostCount(2);
		gpsCacheCount = infoBiz.getNoPostCount(3);

		if(businessCacheCount == 0){
			business_cache_data_size.setVisibility(View.GONE);
		}else{
			business_cache_data_size.setVisibility(View.VISIBLE);
			business_cache_data_size.setText(""+businessCacheCount+" 条未上传");
		}
		if(checkinCacheCount == 0){
			checkin_cache_data_size.setVisibility(View.GONE);
		}else{
			checkin_cache_data_size.setVisibility(View.VISIBLE);
			checkin_cache_data_size.setText(""+checkinCacheCount+" 条未上传");
		}
		if(checkoutCacheCount == 0){
			checkout_cache_data_size.setVisibility(View.GONE);
		}else{
			checkout_cache_data_size.setVisibility(View.VISIBLE);
			checkout_cache_data_size.setText(""+checkoutCacheCount+" 条未上传");
		}
		if(gpsCacheCount == 0){
			gps_cache_data_size.setVisibility(View.GONE);
		}else{
			gps_cache_data_size.setVisibility(View.VISIBLE);
			gps_cache_data_size.setText(""+gpsCacheCount+" 条未上传");
		}

		long recordFileSize = FileOper.getFileListSize(new File(Constants.RECORDING_PATH));

		tv_record_size.setText("文件共"+FileOper.getFileSize(recordFileSize));
		tv_photo_size.setText("文件共0.00 KB");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		isExit = false;
	}

	private void setupViews() {
		// TODO Auto-generated method stub
		business_data_size = (TextView) findViewById(R.id.tv_business_data_size);
		checkin_data_size = (TextView) findViewById(R.id.tv_checkin_data_size);
		checkout_data_size = (TextView) findViewById(R.id.tv_checkout_data_size);
		gps_data_size = (TextView) findViewById(R.id.tv_gps_data_size);
		recorder_data_size = (TextView) findViewById(R.id.tv_recorder_data_size);
		photo_data_size = (TextView) findViewById(R.id.tv_photo_data_size);
		driver_data_size = (TextView) findViewById(R.id.tv_driver_data_size);

		business_cache_data_size = (TextView) findViewById(R.id.tv_business_cache_data_size);
		checkin_cache_data_size = (TextView) findViewById(R.id.tv_checkin_cache_data_size);
		checkout_cache_data_size = (TextView) findViewById(R.id.tv_checkout_cache_data_size);
		gps_cache_data_size = (TextView) findViewById(R.id.tv_gps_cache_data_size);
		tv_record_size = (TextView) findViewById(R.id.tv_record_size);
		tv_photo_size = (TextView) findViewById(R.id.tv_photo_size);

		ll_business = (LinearLayout) findViewById(R.id.ll_business);
		ll_checkin = (LinearLayout) findViewById(R.id.ll_checkin);
		ll_checkout = (LinearLayout) findViewById(R.id.ll_checkout);
		ll_gps = (LinearLayout) findViewById(R.id.ll_gps);
		ll_driver = (LinearLayout) findViewById(R.id.ll_driver);
		ll_recorder = (LinearLayout) findViewById(R.id.ll_recorder);
		ll_photo = (LinearLayout) findViewById(R.id.ll_photo);
		ll_params = (LinearLayout) findViewById(R.id.ll_params);
	}

	private void addListener() {
		// TODO Auto-generated method stub
		ll_business.setOnClickListener(this);
		ll_checkin.setOnClickListener(this);
		ll_checkout.setOnClickListener(this);
		ll_gps.setOnClickListener(this);
		ll_driver.setOnClickListener(this);
		ll_recorder.setOnClickListener(this);
		ll_photo.setOnClickListener(this);
		ll_params.setOnClickListener(this);

		/*business_data_clear.setOnClickListener(this);
		checkin_data_clear.setOnClickListener(this);
		checkout_data_clear.setOnClickListener(this);
		clear_all.setOnClickListener(this);*/
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int type = DownloadDataSelection.TYPE_BUSINESS;
		switch (v.getId()) {
			case R.id.ll_business:
				type = DownloadDataSelection.TYPE_BUSINESS;
				break;
			case R.id.ll_checkin:
				type = DownloadDataSelection.TYPE_CHECKIN;
				break;
			case R.id.ll_checkout:
				type = DownloadDataSelection.TYPE_CHECKOUT;
				break;
			case R.id.ll_gps:
				type = DownloadDataSelection.TYPE_GPS;
				break;
			case R.id.ll_photo:
				type = DownloadDataSelection.TYPE_PHOTO;
				break;
			case R.id.ll_recorder:
				type = DownloadDataSelection.TYPE_RECORDING;
				break;
			case R.id.ll_driver:
				type = DownloadDataSelection.TYPE_SERVICE_CARD;
				break;
			case R.id.ll_params:
				type = DownloadDataSelection.TYPE_DEVICE_PARAMS;
				break;
		}
		Intent intent = new Intent(DownloadData.this, DownloadDataSelection.class);
		intent.putExtra("type", type);
		startActivity(intent);
	}

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub

	}
}
